package com.rental.houses.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rental.common.login.GetIdByToken;
import com.rental.common.result.AjaxResult;
import com.rental.houses.service.RequiredHouseService;
import com.rental.model.entity.house.RequiredHouse;
import com.rental.model.form.house.RequiredHouseForm;
import com.rental.model.form.house.RequiredHousePage;
import com.rental.model.form.house.RequiredReportForm;
import com.rental.model.vo.house.RequiredHouseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.java.Log;
import org.redisson.api.RBatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log
@RestController
@RequestMapping("/required/houses/")
@Tag(name = "RequiredHouseController", description = "个人期望租房信息，可接受价格区间，具体地理位置")
public class RequiredHouseController {

    @Autowired
    private RequiredHouseService requiredHouseService;


    /**
     * 获取当前用户的所有求租房屋信息
     * @param uid
     * @return
     */
    @Operation(summary = "获取当前用户的所有求租房屋信息")
    @GetMapping("/getRequiredHouseListByUid/{uid}")
    public AjaxResult<List<RequiredHouse>> getRequiredHouseListByUid(@PathVariable Long uid){
        return AjaxResult.success(requiredHouseService.getRequiredHouseListByUid(uid));
    }

    /**
     * 添加个人期望租房信息，可接受价格区间，具体地理位置
     * @param requiredHouseForm
     * @return
     */
    @Operation(summary = "个人期望租房信息，可接受价格区间，具体地理位置")
    @PostMapping("/addRequiredHouse")
    public AjaxResult<Boolean> addRequiredHouse(@RequestBody RequiredHouseForm requiredHouseForm){
        return AjaxResult.success(requiredHouseService.addRequiredHouse(requiredHouseForm));
    }


    /**
     * 分页查询个人期望房租列表
     */
    @Operation(summary = "分页查询个人期望房租列表")
    @PostMapping("/getRequiredHouseList")
    public AjaxResult<Page<RequiredHousePage>> getRequiredHouseList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) RequiredHouseForm requiredHouseForm
    ) {
        // 执行分页查询
        AjaxResult<Page<RequiredHousePage>> result = requiredHouseService.getRequiredHouseList(pageNum.longValue(), pageSize.longValue(), requiredHouseForm);
        return result;
    }


    /**
     * 查询个人求租详情信息
     * @param id
     * @return
     */
    @GetMapping("/getRequiredHouseDetail/{id}")
    AjaxResult<RequiredHouseVo> getRequiredHouseDetail(@PathVariable("id") Long id){
        return AjaxResult.success(requiredHouseService.getRequiredHouseDetail(id));
    }

    /**
     * 下架个人求租信息
     * @param id
     * @return
     */
    @Operation(summary = "下架个人求租信息")
    @GetMapping("/deleteRequiredHouse/{id}")
    AjaxResult<Boolean> deleteRequiredHouse(@PathVariable Long id){
        return AjaxResult.success(requiredHouseService.deleteRequiredHouse(id));
    }


    /**
     * 查询个人求租信息
     * @param id
     * @return
     */
    @GetMapping("/getRequiredHouseById/{id}/{userId}")
    AjaxResult<RequiredHouseVo> getRequiredHouseById(@PathVariable Long id,@PathVariable Long userId){
        return AjaxResult.success(requiredHouseService.getRequiredHouseById(id,userId));
    }

    /**
     * 更新个人求租信息
     * @param requiredHouseForm
     * @return
     */
    @Operation(summary = "更新个人求租信息")
    @PostMapping("/updateRequiredHouse")
    public AjaxResult<Boolean> updateRequiredHouse(@RequestBody RequiredHouseForm requiredHouseForm){
        return AjaxResult.success(requiredHouseService.updateRequiredHouse(requiredHouseForm));
    }


    /**
     * 点击举报时候将信息保存
     * @param requiredReportForm
     * @return
     */
    @Operation(summary = "点击举报时候将信息保存")
    @PostMapping("/reportRequiredHouse")
    AjaxResult<Boolean> reportRequiredHouse(@RequestBody RequiredReportForm requiredReportForm){
        return AjaxResult.success(requiredHouseService.reportRequiredHouse(requiredReportForm));
    }


    @Operation(summary = "收藏")
    @GetMapping("/collect/{rid}/{uid}")
    public AjaxResult<Boolean> collect(@PathVariable Long rid,@PathVariable Long uid){
        return AjaxResult.success(requiredHouseService.collect(rid,uid));
    }


    /**
     * 取消收藏
     * @param rid
     * @param uid
     * @return
     */
    @Operation(summary = "取消收藏")
    @GetMapping("/cancelCollect/{rid}/{uid}")
    AjaxResult<Boolean> cancelCollect(@PathVariable Long rid, @PathVariable Long uid){
        return AjaxResult.success(requiredHouseService.cancelCollect(rid,uid));

    }
}

